package com.jxtc.enterprise.hr.controller;

import com.jxtc.enterprise.hr.dto.OrderExportDTO;
import com.jxtc.enterprise.hr.dto.PageOrderByTimeDTO;
import com.jxtc.enterprise.hr.dto.QueryOrderDetailDTO;
import com.jxtc.enterprise.hr.service.HrOrderManagementService;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.hr.vo.HrOrderDetailVO;
import com.jxtc.enterprise.hr.vo.HrOrderVO;
import com.jxtc.enterprise.common.vo.PaginationResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RestController
@RequestMapping("/jxtc/enterprise/order")
@RequiredArgsConstructor
@Slf4j
public class OrderManagementController {

    private final HrOrderManagementService hrOrderManagementService;

    @ApiResponse(responseCode = "200", description = "code 为 200 时，data 字段为包含订单列表的分页信息")
    @Operation(summary = "8.1 根据公司 ID 查询订单分页列表", tags = {"8. 订单管理"}, operationId = "8.1")
    @PostMapping("/list")
    public Result<PaginationResponseVO<HrOrderVO>> pageOrderByTime(@RequestBody PageOrderByTimeDTO dto) {
        log.info("companyId: {}, startTime: {}, endTime: {}, page: {}, size: {}", dto.getCompanyId(), dto.getStartTime(), dto.getEndTime(), dto.getPage(), dto.getSize());
        PaginationResponseVO<HrOrderVO> vo = hrOrderManagementService.pageOrderByCompanyIdAndTime(dto.getCompanyId(), dto.getStartTime(), dto.getEndTime(), dto.getPage(), dto.getSize());
        return ResultGenerator.genSuccessResult(vo);
    }

    @ApiResponse(responseCode = "200", description = "code 为 200 时，data 字段为包含订单详情列表的分页信息")
    @Operation(summary = "8.2 根据公司 ID 和日期查询订单详情列表", tags = {"8. 订单管理"}, operationId = "8.2")
    @PostMapping("/detail")
    public Result<PaginationResponseVO<HrOrderDetailVO>> queryOrderDetail(@RequestBody QueryOrderDetailDTO dto) {
        log.info("companyId: {}, date: {}", dto.getCompanyId(), dto.getDate());
        PaginationResponseVO<HrOrderDetailVO> vo = hrOrderManagementService.pageOrderDetailByCompanyIdAndDate(dto.getCompanyId(), dto.getDate(), dto.getPage(), dto.getSize());
        return ResultGenerator.genSuccessResult(vo);
    }

    @ApiResponse(responseCode = "200", description = "导出成功")
    @Operation(summary = "8.3 根据公司 ID 和日期列表将订单信息导出成 Excel", tags = {"8. 订单管理"}, operationId = "8.3")
    @PostMapping("/export")
    public void export2Excel(HttpServletResponse response, @Valid @RequestBody OrderExportDTO dto) {
        log.info("{}", dto);
        hrOrderManagementService.export2Excel(response, dto);
    }
}
